<?php

namespace yan\zhconversion;

/**
 * This is just an example.
 */
global $zh2Hans, $zh2Hant, $zh2TW, $zh2CN, $zh2SG, $zh2HK;
require_once( dirname(__FILE__) . '/lib/ZhConversion.php');
class Convert
{
    private static $lang_list = array('zh-hans', 'zh-hant', 'zh-cn', 'zh-hk', 'zh-sg', 'zh-tw', 'zh-my', 'zh-mo');
    private static $wpcc_langs = array(
        'zh-hans' => array('zhconversion_hans', 'zh2Hans', '简体中文'),
        'zh-hant' => array('zhconversion_hant', 'zh2Hant', '繁體中文'),
        'zh-cn' => array('zhconversion_cn', 'zh2CN', '大陆简体'),
        'zh-hk' => array('zhconversion_hk', 'zh2HK', '港澳繁體'),
        'zh-sg' => array('zhconversion_sg', 'zh2SG', '马新简体'),
        'zh-tw' => array('zhconversion_tw', 'zh2TW', '台灣正體'),
        'zh-mo' => array('zhconversion_hk', 'zh2MO', '澳門繁體'),
        'zh-my' => array('zhconversion_sg', 'zh2MY', '马来西亚简体'),
        'zh' => array('zhconversion_zh', 'zh2ZH', '中文'),
    );
    public static function to($text, $to_lang='zh-hans'){
        $wpcc_variant = str_replace('_', '-', strtolower(trim($to_lang)));
		
		$data = '';
		if( !empty($wpcc_variant) && 
			in_array($wpcc_variant, self::$lang_list) )
			$data = self::zhconversion($text, $wpcc_variant);
		else $data = $text;
		return $data;
    }
    public static function zhconversion($str, $variant){
        $wpcc_langs = self::$wpcc_langs;
		$method = $wpcc_langs[$variant][0];
		return self::{ $method }($str);
    }
    public static function zhconversion_hant($str) {
		global $zh2Hant;
		return strtr($str, $zh2Hant );
	}

	public static function zhconversion_hans($str) {
		global $zh2Hans;
		return strtr($str, $zh2Hans);
	}

	public static function zhconversion_cn($str) {
		global $zh2Hans, $zh2CN;
		return strtr(strtr($str, $zh2CN), $zh2Hans);
	}

	public static function zhconversion_tw($str) {
		global $zh2Hant, $zh2TW;
		return strtr(strtr($str, $zh2TW), $zh2Hant);
	}

	public static function zhconversion_sg($str) {
		global $zh2Hans, $zh2SG;
		return strtr(strtr($str, $zh2SG), $zh2Hans);
	}

	public static function zhconversion_hk($str) {
		global $zh2Hant, $zh2HK;
		return strtr(strtr($str, $zh2HK), $zh2Hant);
	}

	public static function zhconversion_zh($str) {
		return $str;
	}
}
